package com.cjm.pattern.adapter.vthree;

import com.cjm.pattern.adapter.vonewihnopattern.LogFileOperateApi;
import com.cjm.pattern.adapter.vonewihnopattern.LogModel;
import com.cjm.pattern.adapter.vtwonewinterface.LogDbOperateApi;

import java.util.List;

public class Adapter implements LogDbOperateApi {
    private final LogFileOperateApi adaptee;

    public Adapter(LogFileOperateApi adaptee) {
        this.adaptee = adaptee;
    }

    @Override
    public void createLog(LogModel logModel) {
        List<LogModel> logModels = adaptee.readLogFile();
        logModels.add(logModel);
        adaptee.writeLogFile(logModels);
    }

    @Override
    public void updateLog(LogModel logModel) {
        List<LogModel> logModels = adaptee.readLogFile();
        for (int i = 0;i <logModels.size();i++) {
            if (logModels.get(i).getLogId().equals(logModel.getLogId())) {
                logModels.set(i, logModel);
            }
        }
        adaptee.writeLogFile(logModels);
    }

    @Override
    public void removeLog(LogModel logModel) {
        List<LogModel> logModels = adaptee.readLogFile();
        logModels.remove(logModel);
    }

    @Override
    public List<LogModel> getAllLog() {
        return adaptee.readLogFile();
    }
}
